package com.huifaapp.mall.activity.Homefragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.InvitationVIPActivity;
import com.huifaapp.mall.activity.LoginActivity;
import com.huifaapp.mall.activity.ShopYouhuiShareActivity;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.InvitationVIP;
import com.huifaapp.mall.bean.ShareAppBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.UtilsImage;
import com.nostra13.universalimageloader.utils.L;
import com.squareup.okhttp.Request;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class OneShareActivity extends AppCompatActivity implements View.OnClickListener{


    private ImageView im_close;
    private LinearLayout ll_shareApp_weixin;
    private LinearLayout ll_pyq;
    private LinearLayout ll_qqhy;
    private LinearLayout ll_weibo;
    ShareAppBean list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_share);

        initView();
    }

    private void initView() {
        im_close = (ImageView) findViewById(R.id.share_close);
        im_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ll_shareApp_weixin= (LinearLayout) findViewById(R.id.ll_shareApp_weixin);
        ll_shareApp_weixin.setOnClickListener(this);
        ll_pyq= (LinearLayout) findViewById(R.id.ll_pyq);
        ll_pyq.setOnClickListener(this);
        ll_qqhy= (LinearLayout) findViewById(R.id.ll_qqhy);
        ll_qqhy.setOnClickListener(this);
        ll_weibo= (LinearLayout) findViewById(R.id.ll_weibo);
        ll_weibo.setOnClickListener(this);
        LoadShareApp();
    }
    private void LoadShareApp(){
        Log.e("分享App","url=============="+ ApiManager.PROJECT_SHAREAPP);
        OkHttpUtils.get().url(ApiManager.PROJECT_SHAREAPP+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+AUniqueIdentifier.getDeviceId(OneShareActivity.this))
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        list = gson.fromJson(response,ShareAppBean.class);
                        if(list.getCode().equals("10000")){
                            Toast.makeText(OneShareActivity.this,list.getMsg(),Toast.LENGTH_SHORT).show();
                        }else if(list.getCode().equals("10001")){
                            Intent intent=new Intent(OneShareActivity.this,LoginActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(OneShareActivity.this,list.getMsg(),Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_shareApp_weixin:
                final UMWeb web = new UMWeb(list.getData().getLink().getUrl());
                web.setTitle("京选友惠");//标题
                web.setThumb(new UMImage(OneShareActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "1",null,web,null);
                break;
            case R.id.ll_pyq:
                final UMWeb web1 = new UMWeb(list.getData().getLink().getUrl());
                web1.setTitle("京选友惠");//标题
                web1.setThumb(new UMImage(OneShareActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web1.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "2",null,web1,null);
                break;
            case R.id.ll_qqhy:
                final UMWeb web2 = new UMWeb(list.getData().getLink().getUrl());
                web2.setTitle("京选友惠");//标题
                web2.setThumb(new UMImage(OneShareActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web2.setDescription(list.getData().getLink().getContent());//描述
                shareUrl( "3",null,web2,null);
                break;
            case R.id.ll_weibo:
                final UMWeb web3 = new UMWeb(list.getData().getLink().getUrl());
                web3.setTitle("京选友惠");//标题
                web3.setThumb(new UMImage(OneShareActivity.this, list.getData().getLink().getImgUrl()));  //缩略图
                web3.setDescription(list.getData().getLink().getContent()
                );//描述
                shareUrl( "4",null,web3,null);
                break;
        }
    }
    /**
     * 分享链接
     */

    private void shareUrl(String type, String con, UMWeb web, UMImage image) {

        android.util.Log.v("shiran","-----------shareUrl()----------");


        UMShareListener umShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
//                if (dialog!=null&&!dialog.isShowing()) {
//                    dialog.show();
//                }
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享成功");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享成功");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享成功");
                } else if(share_media ==SHARE_MEDIA.SINA){
                    ToastUtils.showShortSafe("新浪微博分享成功");
                }else {
                    ToastUtils.showShortSafe("分享成功");

                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享失败");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享失败");
                }else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享失败");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享失败");
                } else {
                    ToastUtils.showShortSafe("分享失败");

                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享取消");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享取消");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享取消");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享取消");
                } else {
                    ToastUtils.showShortSafe("分享取消");

                }
            }
        };

        if ("1".equals(type)) {  //分享微信
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.WEIXIN).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();

        }else  if ("2".equals(type)) { //分享朋友圈

            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();

        }else if("3".equals(type)){//分享qq
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.QQ).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        }else if("4".equals(type)){//分享微博
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.SINA).
                    setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
